<?php
namespace GuessNewSong\Controller;
use Common\Controller\GuessNewSongController;

class UntiController extends GuessNewSongController {

    const AppID     =   'wx370386b7eb0c04bc';       //wx370386b7eb0c04bc
    const AppSecret =   '25d7798f008b06ec3641d5e709599600'; //25d7798f008b06ec3641d5e709599600

    public function test_unit()
    {
        exit;
        $question_url           =   'https://api.beikg.cn/GuessNewSong/index/new_problem';
        $post_data['uid']       =   2;
        $post_data['appid']     =   'wx370386b7eb0c04bc';

        for ($x=0; $x<=40; $x++)
        {
            $return_data = $this->test_curl($question_url,$post_data,true);

            if (! empty($return_data['data']['question']['4']))
            {
                $red_url    =   'https://api.beikg.cn/GuessNewSong/index/new_answer';
                $requst_data['uid']             =   2;
                $requst_data['aswer_id']        =   $return_data['data']['question']['4']['aswer_id'];
                $requst_data['answer_tg']       =   $return_data['data']['question']['4']['aswer_id'];
                $requst_data['id']              =   $return_data['data']['question']['4']['id'];
                $requst_data['number']          =   5;

                $result = $this->test_curl($red_url,$requst_data,true);

                if (! empty($result['data']['challenge_id']))
                {
                    $red_data                   =   array();
                    $red_data['challenge_id']   =   $result['data']['challenge_id'];
                    $red_data['uid']            =   2;

                    $red_url2   =   'https://api.beikg.cn/GuessNewSong/user/award_total';
                    $this->test_curl($red_url2,$requst_data,false);

                }

            }
        }

    }

    public function test_curl($url,$post_data,$ret  =   false )
    {
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch,CURLOPT_TIMEOUT,3);  //定义超时3秒钟
        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        //执行并获取url地址的内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);

        if (! empty($ret))
        {
            return json_decode($output,true);
        }
    }

    public function set_redis()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1',6379);
        $allow_key      =   'timed';
        $redis->set($allow_key,'true',10);

    }

    public function read_redis()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1',6379);
        $allow_key      =   'timed';
        $result     =   $redis->get($allow_key);
        echo $result;
    }
}